<!--事故统计柱状图-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="zhu_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null,
      allData: null, //服务器返回的数据
      startValue: 0, //区域缩放起点值
      endValue: 2, //区域缩放终点值
      timeId: null, //定时器id
    };
  },
  props: {
    fenqu: String,
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.zhu_ref, "chalk");
      const initOption = {
        backgroundColor: "", //设置无背景色
        legend: {},
        tooltip: {},
        xAxis: { type: "category" },
        yAxis: {},
        // Declare several bar series, each will be mapped
        // to a column of dataset.source by default.
        series: [{ type: "bar" }, { type: "bar" }, { type: "bar" }],
        color: ["#00f2f1", "#ed3f35", "#ffd200"],
        //向右移动
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        // console.log("鼠标移入");
        this.timeId && clearInterval(this.timeId);
      });
      this.chartInstance.on("mouseout", () => {
        // console.log("鼠标移出");
        this.startInterval();
      });
    },
    async getData() {
      const res = await this.$http.get("/ai/year_warn", {
        params: {
          fenqu: this.fenqu,
        },
      });
      this.allData = res.data;
      // console.log(this.allData);
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const dataOption = {
        dataset: {
          source:
            //   {
            //     product: "2015",
            //     异常行为: 43.3,
            //     违章行为: 85.8,
            //     聚集报警: 93.7,
            //   },
            //   {
            //     product: "2016",
            //     异常行为: 43.3,
            //     违章行为: 85.8,
            //     聚集报警: 93.7,
            //   },
            //   {
            //     product: "2017",
            //     异常行为: 43.3,
            //     违章行为: 85.8,
            //     聚集报警: 93.7,
            //   },
            //   {
            //     product: "2018",
            //     异常行为: 43.3,
            //     违章行为: 85.8,
            //     聚集报警: 93.7,
            //   },
            // ],
            this.allData.map((item) => {
              return {
                product: item.year,
                异常行为: item.fault,
                违章行为: item.illegal,
                聚集报警: item.too_much,
              };
            }),
        },
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.zhu_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      this.timeId = setInterval(() => {
        this.startValue = this.startValue + 1;
        this.endValue = this.endValue + 1;
        if (this.endValue > 3) {
          this.startValue = 0;
          this.endValue = 2;
        }
        this.initChart();
      }, 2000);
    },
  },
};
</script>

<style>
</style>